package homework.work;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Scanner;

public class DateCalculator {
    public static void main(String[] args)
    {

        Scanner scanner = new Scanner(System.in);
        // 输入月份并输出月历
        System.out.print("请输入月份(1 - 12): ");
        int month = scanner.nextInt();
        printCalendar(month);

        // 输入日期并显示是星期几
        System.out.print("请输入日期(格式: yyyy - MM - dd): ");
        scanner.nextLine();
        String dateStr = scanner.nextLine();
        LocalDate date = LocalDate.parse(dateStr);
        System.out.println("该日期是星期 " + getDayOfWeek(date));

        // 输入两个日期并计算天数差
        System.out.print("请输入第一个日期(格式: yyyy - MM - dd): ");
        String date1Str = scanner.nextLine();
        LocalDate date1 = LocalDate.parse(date1Str);
        System.out.print("请输入第二个日期(格式: yyyy - MM - dd): ");
        String date2Str = scanner.nextLine();
        LocalDate date2 = LocalDate.parse(date2Str);
        System.out.println("两个日期之间相差 " + getDaysBetween(date1, date2) + " 天");

        scanner.close();
    }
    public static void printCalendar(int month) {
        //创建2024年某一月从第一天开始的日历
        LocalDate date = LocalDate.of(2024, month, 1);
        int daysInMonth = date.lengthOfMonth();
        DayOfWeek firstDayOfMonth = date.getDayOfWeek();
        //
        int firstDayIndex = firstDayOfMonth.getValue() == 7? 0 : firstDayOfMonth.getValue();
        //打印日历表头
        System.out.println(" 日 一 二 三 四 五 六");
        //对日历第二行加空格
        for (int i = 0; i < firstDayIndex; i++) {
            System.out.print("   ");
        }
        for (int i = 1; i <= daysInMonth; i++) {
            System.out.printf("%3d", i);
            if ((firstDayIndex + i) % 7 == 0) {
                System.out.println();
            }
        }
        System.out.println();
    }

    public static String getDayOfWeek(LocalDate date) {
        DayOfWeek a = date.getDayOfWeek();//   一周七天的枚举（给出时间返回周几）
        switch (a) {
            case MONDAY:
                return "一";
            case TUESDAY:
                return "二";
            case WEDNESDAY:
                return "三";
            case THURSDAY:
                return "四";
            case FRIDAY:
                return "五";
            case SATURDAY:
                return "六";
            case SUNDAY:
                return "日";
            default:
                return "未知";
        }
    }

    public static long getDaysBetween(LocalDate startDate, LocalDate endDate) {
            return endDate.toEpochDay() - startDate.toEpochDay();//返回第一个时间减去最后一个的时间

    }
}